<?php
/********************
 *@param function:管理員管理 model
 *@param datetime 2011-6-7
 *@param author solgh
 ********************/
class Admins_mdl extends CI_model{
  //操作的表.
  const TAB_NAME='clickrcms_admin';
  const TAB_GROUP='clickrcms_admin_group';
  function __construct(){
    parent::__construct();
    log_message('debug','admins_mdl Model class Initialized');
  }

  /****
   *查詢所admins,加分页
   *$data :查询参数.
   */
  public function get_admins($data=array()){
    if(!empty($data)){
      extract($data);
      isset($field) && isset($sort)?$this->db->order_by($field.' '.$sort):$this->db->order_by('n.add_time ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($login_name) && !empty($login_name)?$this->db->like('n.login_name',$login_name):'';
      isset($email) && !empty($email)?$this->db->like('n.email',$email):'';
      isset($group_name) && !empty($group_name)?$this->db->like('nd.name',$group_name):'';
      $this->db->select('n.*,nd.name AS group_name');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_GROUP.' AS nd', 'n.group_id = nd.id','LEFT');
      return $this->db->get();
    }
  }
  /****
   * 統計所有的admins
   */
  public function totle_admins($data=array()){
    if(!empty($data)){
      extract($data);
      isset($status) && !empty($status)?$this->db->where('n.status',$status):'';
      isset($login_name) && !empty($login_name)?$this->db->like('n.login_name',$login_name):'';
      isset($email) && !empty($email)?$this->db->like('n.email',$email):'';
      isset($group_name) && !empty($group_name)?$this->db->like('nd.name',$group_name):'';
      $this->db->select('n.*,nd.name AS group');
      $this->db->from(self::TAB_NAME.' AS n');
      $this->db->join(self::TAB_GROUP.' AS nd', 'n.group_id = nd.id','LEFT');
      return $this->db->count_all_results();
    }else{
      return $this->db->count_all(self::TAB_NAME);
    }
  }
  /****
   * 判斷是否登錄
   */
  function check_online($admin_id=0){
    if(!empty($admin_id)){
      $this->db->where('id',$admin_id);
      $this->db->select('id');
      $query = $this->db->get(self::TAB_NAME);
      $num = $query->num_rows();
      $query->free_result();
      return ($num > 0) ? TRUE : FALSE;
    }else{
      return false;
    }
  }

  /****
   * 驗證登陸
   */
  function check_admin($admin_array=array()){
    $data=array();
    $this->db->where($admin_array);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0)
    {
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }
  
  /****
   *查詢admins,無緩存模式
   *$data :查询参数.
   */
  public function get_core_admins($data=array()){
    if(!empty($data)){
      extract($data);
      $this->db->order_by('sort_order ASC');
      isset($limits) && isset($offset)?$this->db->limit($limits,$offset):'';
      isset($status) && !empty($status)?$this->db->where('status',$status):'';
      $this->db->cache_off();
      $query=$this->db->get(self::TAB_NAME);
      $this->db->cache_on();
      return $query;
    }
  }
  /*****
   * 根據ID來刪除
   */
  public function delete_admins($admins_id=0){
    if(!empty($admins_id)){
      $this->db->where("id",$admins_id);
      $this->db->delete(self::TAB_NAME);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }

  /****
   *根据ID来查询得到一个信息.
   *$id :要查詢的ID
   */
  public function get_admins_by_id($id){
    $data=array();
    $this->db->where('id',$id);
    $get_result=$this->db->get(self::TAB_NAME);
    if($get_result->num_rows()>0)
    {
      $data=$get_result->row_array();
    }
    $get_result->free_result();
    return $data;
  }
    public function get_member_palyers(){
        $palyers=array();
        $this->db->where_in('group_id',array(3,4));
        $get_result = $this->db->get(self::TAB_NAME);
        if($get_result->num_rows()>0)
        {
            $data=$get_result->result_array();
            foreach ($data as $v){
                if(!empty($v['palyer_id'])){
                    $palyers[] = $v['palyer_id'];
                }
            }
        }
        $get_result->free_result();
        return $palyers;
    }
    /****
     *根据token来查询得到一个信息.
     *$id :要查詢的ID
     */
    public function get_admins_by_token($token){
        $data=array();
        $this->db->where('token',$token);
        $get_result=$this->db->get(self::TAB_NAME);
        if($get_result->num_rows()>0)
        {
            $data=$get_result->row_array();
        }
        $get_result->free_result();
        return $data;
    }
  /***
   * 修改一個admins
   * @param array $data
   * @param int  $c_id
   * return :受影响的行数
   */
  public function edit_admins($data=array(),$b_id=0){
    if(!empty($data) && !empty($b_id)){
      $this->db->where('id',$b_id);
      $this->db->update(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  /***
   * 新增一個 admins
   */
  public function add_admins($data=array()){
    if(!empty($data)){
      $this->db->insert(self::TAB_NAME,$data);
      return ($this->db->affected_rows()>0)?true:false;
    }
  }
  public function get_user_admin($username,$password){
    $this->db->where('login_name', $username);
    $this->db->where('login_pwd', $password);
    //$this->db->select('clickrcms_admin.*');

    $query=$this->db->get(self::TAB_NAME);
    $return=array();
    if($query->num_rows()>0){
        $return=$query->row_array();
    }
    $query->free_result();
    return $return;
  }

}
